<?php
/**
 * Created by PhpStorm.
 * User: Apache
 *  展商  展品管理
 *
 */

namespace app\api\controller\basemanage;
use app\api\controller\common\ApiBase;
use think\Db;
use think\Model;
use app\api\model\Admin as AdminModel;
use app\api\controller\common\CommonPlus;
use think\Controller; // dengh quxiao
// Controller  ApiBase （dengl）
class Zshuikan extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        //$this->model = model('CompanyPublic');
    }


    /**
     * 根据公海id获得获取
     * @return \think\response\Json|void
     *  http://dxshowxya.for.pub/index.php?s=/api/basemanage/Yuyuemanage/getList
     */

     public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        if($result){
            $tabName=$result['create_tablename'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
     }

    /**
     *
     * @return \think\response\Json|void
     *  comppub
     *
     */

    public function getList(){
        $page = $this->request->param('page');
        $page = max(1,intval($page));
        $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $exhibition_id =$this->request->param('exhibition_id');
        $status=$this->request->param('status')==''?1:$this->request->param('status');
        if(empty($exhibition_id)){
            //return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }

        $status = input('status');
        //back字段 0代表正常 1代表退回数据 2 代表自提 3代表分配
        //status前端传入1代表 全部 2代表 不展示退回数据 3代表 只展示退回数据
        $param="";

        $keyword =input('keyword');
        if (!empty($keyword)) {
            $param.=" and (title like '%$keyword%' or title_en like '%$keyword%') ";
        }
        $sql2="select * ";
        $sql2.=" from crm_zsgoodszp where 1=1 $param";
        $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
        //var_dump($sql2);die();
        $list=Db::query($sql2);
        
        $sql21=" select count(id) as count1 ";
        $sql21.=" from crm_zsgoodszp where 1=1 $param ";
        $total1=Db::query($sql21);
        $total=$total1[0]['count1']; //st
        // 1
        foreach($list as &$val){
            $val['pic']=unserialize($val['pic']);
        }
        unset($val);

        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
    }


    /**
     * 添加
     * @return \think\response\Json|void
     *  数组形式 
     */
    public function add_01(){
        $com =new CommonPlus();
        $exhibition_id = $this->request->param('exhibition_id');
        $company_id = $this->request->param('company_id');
        if(empty($exhibition_id)){
           // return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        if(empty($company_id)){
            //return json(['code'=>0,'msg'=>'请选择企业','data'=>'']);
        }
        // com
        $data['create_time']=time();
        //
        $data['title']=$this->request->param('title');   // 新品名称中文
        $data['title_en']=$this->request->param('title_en');
        $data['price']=$this->request->param('price');
        $data['intro']=$this->request->param('intro'); // 新品介绍 中文
        $data['intro_en']=$this->request->param('intro_en');
        $data['pic']=$this->request->param('pic'); // @ 如果多图
        $picarr=explode('@',$data['pic']);
        if(is_array($picarr)){
            $data['pic']=serialize($picarr);// 看怎么拼
        }
        //  $detaildata1 = input('D_detail'); // 数组
        // $detaildata = json_decode(json_encode(($detaildata22)),TRUE);
        // $detaildata = json_decode($detaildata1, TRUE);

        //var_dump(1111);die;
        //$tabName=$this->getTabName($exhibition_id);
        //$company_id = explode(',',$company_id);
        //$data=array();
        $res=$com->insert_com_dan($data, '', 'zsgoodszp');
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }


    /**
     *  数组形式
     *      * (json_decode(htmlspecialchars_decode($_GPC['datas']),true));die();
     *   // 1.2 版本 有些东西是下拉选择
     *  "[{"id":"16","title":"无缝钢管","pid":"5","guige":"D22","num":"11"}]"  ke ce
     */
    public function add(){
        $com =new CommonPlus();
        $exhibition_id = $this->request->param('exhibition_id');
        $company_id = $this->request->param('company_id');
        if(empty($exhibition_id)){
           // return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        if(empty($company_id)){
            //return json(['code'=>0,'msg'=>'请选择企业','data'=>'']);
        }
        // com
        $data['create_time']=time();
        //
        
        $dataarr=$this->request->param('goodszp');
        $datas = json_decode($dataarr, TRUE);
        if(is_array($datas)){
      
            foreach($datas as $val1){

                $data['title']=$val1['title'];   // 新品名称中文
                $data['title_en']=$val1['title_en'];
                $data['price']=$val1['price'];
                $data['intro']=$val1['intro'];// 新品介绍 中文
                $data['intro_en']=$val1['intro_en'];
                $data['pic']=serialize($val1['pic']); // @ 如果多图
                // $picarr=explode('@',$data['pic']);
                // if(is_array($picarr)){
                //     $data['pic']=serialize($picarr);// 看怎么拼
                // }
                // var_dump($data);die;
                $res=$com->insert_com_dan($data, '', 'zsgoodszp');
                if(!$res){
                    return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
                }

            }

        }

       

        
        //  $detaildata1 = input('D_detail'); // 数组
        // $detaildata = json_decode(json_encode(($detaildata22)),TRUE);
        // $detaildata = json_decode($detaildata1, TRUE);

        //var_dump(1111);die;
        //$tabName=$this->getTabName($exhibition_id);
        //$company_id = explode(',',$company_id);
        //$data=array();
       
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }


    /**
     * 删除
     */
    public function del(){
        $com =new CommonPlus();
        $id = $this->request->param('id');
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $tabName='zsgoodszp';
        $r = db($tabName)->where(['id'=>$id])->find();
        if(!$r){
            return json(['code'=>0,'msg'=>'记录不存在','data'=>'']);
        }

        $where['id'] = $id;
        $data['is_del']=1;
        $r=$com->edit_com_fxlist($data, $where, $tabName);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }
    /**
     * 批量删除公海
     */
    public function delall(){ 
        $id = $this->request->param('project_id');
        $ids = explode(',',$id);
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        foreach ($ids as $id){
            $r = $this->model->findWhere(['id'=>$id]);
            if(!$r){
                return json(['code'=>0,'msg'=>'id:'.$id.'记录不存在,批量删除失败','data'=>'']);
            }
            if($r['status']==2 || $r['status']==3){
                return json(['code'=>0,'msg'=>'id:'.$id.'记录已分配，批量删除失败','data'=>'']);
            }
        }

 

    }


    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
        $com =new CommonPlus();
        // com
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        //$create_uid  = $this->admin['id'];
        $data['update_time']=time();
       // $selfcontent = $this->request->param('selfcontent'); //自定义表单 理论上只有新增 才有 。。让前台隐藏吧


        // com
        $data['title']=$this->request->param('title');   // 姓名
        $data['title_en']=$this->request->param('title_en');
        $data['price']=$this->request->param('price');
        $data['intro']=$this->request->param('intro');
        $data['intro_en']=$this->request->param('intro_en');
        $data['pic']=$this->request->param('pic'); // @ 如果多图
        $picarr=explode('@',$data['pic']);
        if(is_array($picarr)){
            $data['pic']=serialize($picarr);// 看怎么拼
        }


        $where['id'] = $id;
        $tbname="zsgoodszp";
        //$data2=array('pwd'=>md5($pwd),'account'=>'cccccccccc');
        $where2=array('id'=>$id,'company_name'=>'test');
        $r=$com->edit_com_fxlist($data, $where, $tbname);

        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

    // 明细
    public function getDetail()
    {
        $com =new CommonPlus();
        // com
        $company_id1 = $this->request->param('company_id1');// 依据现有的东西来
        if (empty($company_id1)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $where['id'] = $company_id1;
        $tbname="company";
        $r=$com->sel_com_one('222', $where, $tbname);

        //$r['question_content_sel']=unserialize($r['question_content_sel']);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }else{
            $r['country_chukou']=unserialize($r['country_chukou']);
            $r['doselfbrander_info']=unserialize($r['doselfbrander_info']); 

        }

        return json(['code' => 1, 'msg' => '操作成功','data'=>$r]);
    }


     /**
     * @return \think\response\Json
     *  会刊录入========
     *   根据公司id 这个是展商表还是基础库呢
     */
    public function editCompany()
    {
        $com = new CommonPlus();
        // com
      //  $id = $this->request->param('id'); //必须
        $company_id = input('company_id1'); // 展商id 展商表。有此字段
        $create_tablename = input('create_tablename'); // 更新展商表用
        //$create_uid  = $this->admin['id'];
        //$selfcontent = $this->request->param('selfcontent'); //自定义表单 理论上只有新增 才有 。。让前台隐藏吧
        // com 
 

        $company_name = $this->request->param('company_name'); //公司中文名
        $company_address = $this->request->param('company_address'); //地址   
        $company_name_en = $this->request->param('company_name_en'); //    公司英文文名
        $company_describe = $this->request->param('company_describe'); //简介   
        $tel = $this->request->param('tel');   //固定电话
        $link = $this->request->param('link');
        $product_brand = $this->request->param('product_brand');   //产品品牌
        $application_area = $this->request->param('application_area');    //应用领域  字段？？ 有两个
        $mobile = $this->request->param('mobile');
        $email = $this->request->param('email');
        $qq = $this->request->param('qq');
        $wechat = $this->request->param('wechat');
        $msn = $this->request->param('msn');
        $product_describe = $this->request->param('product_describe');   //产品介绍
        $product_positioning = $this->request->param('product_positioning');  //定位
        $product_keyword = $this->request->param('product_keyword');    //关键字
        //$application_area = $this->request->param('application_area');  //应用领域 下拉框？还是？？ 两个字段页面
        $remark = $this->request->param('remark');
        $category_id = $this->request->param('category_id');  //一级 分类
        $second_category_id = $this->request->param('second_category_id');     //二级 分类
        $product = $this->request->param('product');  //产品
        $capacitys = $this->request->param('capacitys');  //年产能
        $sale_fee = $this->request->param('sale_fee'); //年销售总额:
        $sale_pb = $this->request->param('sale_pb'); //pb年PB销售占比
        $pb_time = $this->request->param('pb_time');  //进入pb时间
        $pb_customer = $this->request->param('pb_customer'); //主要pb客户
        $country = $this->request->param('country');  //国家
        $country_code = $this->request->param('country_code'); //国家代码
        $area = $this->request->param('area'); //区域
        $province = $this->request->param('province');  //省份
        $area_city = $this->request->param('area_city');  //地级市
        $area_town = $this->request->param('area_town'); //县市      
        // 个人信息
        $surname_cn = $this->request->param('surname_cn');  //中文 姓
        $surname_en = $this->request->param('surname_en');  //英文 姓
        $name_cn = $this->request->param('name_cn');     // 中文名
        $name_en = $this->request->param('name_en'); // 英文名
        $call_name_cn = $this->request->param('call_name_cn'); // 称谓 中
        $call_name_en = $this->request->param('call_name_en'); //称谓 英文
        $positions_cn = $this->request->param('positions_cn');  // 职务 中
        $positions_en = $this->request->param('positions_en'); //职务
        $department_cn = $this->request->param('department_cn'); // 部门中
        $department_en = $this->request->param('department_en'); //部门 英
        $tel_person_code = $this->request->param('tel_person_code');  // 区号
        $tel_person = $this->request->param('tel_person'); // 电话
        $tel_person_history = $this->request->param('tel_person_history'); //原始号码
        $mobile_person = $this->request->param('mobile_person'); //个人手机         
        $fax_person = $this->request->param('fax_person');  // 传真
        $fax_history = $this->request->param('fax_history');  // 原始传真
        $email_person = $this->request->param('email_person');  // email 个人
        $mobile_status = $this->request->param('mobile_status');  // 电话已核对 是需要填写数字？？
        $addr_person = $this->request->param('addr_person');  // 地址 个人
        $info_level = $this->request->param('info_level'); // 信息质量 
        $data_from = $this->request->param('data_from');  // 数据来源
        $duties = $this->request->param('duties');  // 岗位职责
        $job_nature = $this->request->param('job_nature');  // 工作性质
        $company_ID = $this->request->param('company_ID'); //公司id 
        $persontype = $this->request->param('persontype'); //人员类别
        $industrys = $this->request->param('industrys'); // 行业           
        $company_nature = $this->request->param('company_nature');  // 公司性质 
        $qualifications = $this->request->param('qualifications');  // 资质 
        $company_form = $this->request->param('company_form');  // 公司形态      
        $business = $this->request->param('business');  // 业务性质
        $system_status = $this->request->param('system_status');  // 系统状态
        $id_card = $this->request->param('id_card');  // 证件号码
        $chau = $this->request->param('chau');  // 所属洲
        $ishzselfbrander = $this->request->param('ishzselfbrander');  // 是否合作过自有品牌客户
        $ishzselfbrand = $this->request->param('ishzselfbrand');  // 是否合作过自有品牌    
        $label = $this->request->param('label');  //   
        $label_2 = $this->request->param('label_2');  //
        $logo = $this->request->param('logo');  //  logo
        $is_chukou = $this->request->param('is_chukou');  // 是否出口
        $country_chukou = $this->request->param('country_chukou');  // 出口国家
        $country_chukou = serialize(json_decode($country_chukou, TRUE)); // 序列

        $is_card_sheng = $this->request->param('is_card_sheng');  //  是否获取省级证书  的
        $info_card_sheng=$this->request->param('info_card_sheng');// 证书信息
        //
        $isdoselfbrander=$this->request->param('isdoselfbrander');// 是否做过自由品牌
        $doselfbrander_info=$this->request->param('doselfbrander_info');// 自由品牌信息
        $doselfbrander_info = serialize(json_decode($doselfbrander_info, TRUE)); // 序列 
        
        $data = array(
            //'create_uid' => $create_uid, 'selfcontent' => $selfcontent, 
            'update_time' => time(),'remark' => $remark, 'company_name' => $company_name,
            'company_name_en' => $company_name_en, 'company_describe' => $company_describe, 'tel' => $tel, 'link' => $link,
            'product_brand' => $product_brand, 'application_area' => $application_area, 'mobile' => $mobile,
            'email' => $email, 'qq' => $qq, 'wechat' => $wechat, 'msn' => $msn, 'product_describe' => $product_describe,
            'product_positioning' => $product_positioning, 'product_keyword' => $product_keyword, 'remark' => $remark,
            'category_id' => $category_id, 'second_category_id' => $second_category_id, 'product' => $product,
            'capacitys' => $capacitys, 'sale_fee' => $sale_fee, 'sale_pb' => $sale_pb, 'pb_time' => $pb_time,
            'pb_customer' => $pb_customer, 'country' => $country, 'country_code' => $country_code, 'province' => $province,
            'area' => $area, 'area_city' => $area_city, 'area_town' => $area_town,
            'surname_cn' => $surname_cn, 'name_cn' => $name_cn, 'surname_en' => $surname_en,
            'name_en' => $name_en, 'call_name_cn' => $call_name_cn, 'call_name_en' => $call_name_en,
            'positions_cn' => $positions_cn, 'positions_en' => $positions_en, 'department_cn' => $department_cn,
            'department_en' => $department_en, 'tel_person_code' => $tel_person_code, 'tel_person' => $tel_person,
            'tel_person_history' => $tel_person_history, 'mobile_person' => $mobile_person, 'fax_person' => $fax_person,
            'fax_history' => $fax_history, 'email_person' => $email_person, 'mobile_status' => $mobile_status,
            'addr_person' => $addr_person, 'info_level' => $info_level, 'data_from' => $data_from,
            'duties' => $duties, 'job_nature' => $job_nature, 'company_ID' => $company_ID,
            'persontype' => $persontype, 'industrys' => $industrys, 'company_nature' => $company_nature, 'qualifications' => $qualifications,
            'company_form' => $company_form, 'business' => $business, 'system_status' => $system_status,
            'id_card' => $id_card, 'chau' => $chau, 'ishzselfbrander' => $ishzselfbrander, 'ishzselfbrand' => $ishzselfbrand,
            'label' => $label, 'label_2' => $label_2,'company_address'=>$company_address,'logo' => $logo,
            'is_chukou' => $is_chukou,'country_chukou' => $country_chukou,'is_card_sheng' => $is_card_sheng,'info_card_sheng'=>$info_card_sheng,
            'isdoselfbrander'=>$isdoselfbrander,'doselfbrander_info'=>$doselfbrander_info
        );
        //=======

        // 基础库 跟展商 不一样把？？表没有

        //检测是不是 当前公司名称
        $com=new CommonPlus();
        $where1=array('id'=>$company_id );
        $tbname='company';
        $info2=$com->sel_com_one('',$where1,$tbname);

        if(!$info2){
            return json(['code' => 0, 'msg' => '未查询到相关展商信息']);
        }
        //var_dump($info2);die;

        // 验证电话 企业  排除 删除
        if (!empty($company_name)) {
            if ($info2['company_name'] == $company_name) {

            } else {
                $where1=array('company_name'=>$company_name,'is_delete' => 0 );
                $tbname='company';
                $ckinfo=$com->sel_com_one('',$where1,$tbname); 
                $msg = "公司名已存在!";

                if ($ckinfo) {
                    return json(['code' => 0, 'msg' => $msg]);
                }
            }
        }

        if (!empty($tel)) {
            if ($info2['tel'] == $tel) {
            } else {               

                $where1=array('tel'=>$tel,'is_delete' => 0 );
                $tbname='company';
                $ckinfo=$com->sel_com_one('',$where1,$tbname); 
                $msg = "公司固定电话号码已存在!";

                if ($ckinfo) {
                    return json(['code' => 0, 'msg' => $msg]);
                }


            }
        }

        if (!empty($mobile)) {
            if ($info2['mobile'] == $mobile) {
            } else {              

                $where1=array('mobile'=>$mobile,'is_delete' => 0 );
                $tbname='company';
                $ckinfo=$com->sel_com_one('',$where1,$tbname); 
                $msg = "公司手机号码已存在!";

                if ($ckinfo) {
                    return json(['code' => 0, 'msg' => $msg]);
                }
            }
        }
        
       // var_dump($data);die;
        $where2=array('id'=>$company_id);
        $res=$com->edit_com_fxlist($data, $where2, $tbname);

        if (!$res) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        /**
         * 企业管理 操作日志
         */
 
        //$datalog=array('admin_id'=>$this->admin['id'],'admin_name'=>$this->admin['realname'],'content'=>'修改：ID为'.$id."表单内容",'ip' => request()->ip(),'create_time' => time());
        //$wherelog=array('create_time' => time());
        //$tbname="company_edlog";       
        //$re_log=$com->insert_com_fxlist($datalog, $wherelog, $tbname);

        return json(['code' => 1, 'msg' => '操作成功']);
    }

 


    // the end


}
